package com.ww.juc.futureandcallable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyCallableTest {

	public static void main(String[] args) {
		try {
			Callable<String> callable = new MyCallableImpl(100);
			ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 3, 5L, TimeUnit.SECONDS, new LinkedBlockingDeque());
			// 方法submit()不仅可以传入Callable对象，还可以传入Runnable对象，说明submit()方法支持有返回值和无返回值的功能
			Future<String> future = executor.submit(callable);
			System.out.println("main A " + System.currentTimeMillis());
			// 方法get()具有阻塞特性
			System.out.println(future.get());
			System.out.println("main B " + System.currentTimeMillis());
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
